package nx2.behaviors;

import java.lang.Comparable;
/**
 * The Base class for all behaviors. This API implements potential fields for
 * navigation. That is why the Vector2d class of the nx.utils package is extensively
 * used. Remember to use also the arbiters located at the nx.arbitrators folder.
 * 
 * @author Alejandro Pustowka
 */
public abstract class NxBehavior implements Comparable {

    /**
     * The gain for the output of this behavior. Should be a value between 0 and 1.
     */
    private float gain = 1;

    /**
     * A behavior can be disabled or enabled.
     */
    public boolean enabled = true;

    /**
     * Each behavior has a particular name.
     */
    private String name;

    /**
     * Priority used by the Arbitrator to define the total output.
     */
    private int priority = 100;

    /**
     * Contains the index for the next instanciated behavior.
     */
    static int nextIndex = 0;

    /**
     * Index of this behavior.
     */
    int index = 0;

    /**
     * Constructor.
     * @param name Name of the behavior, useful to identify it in arbiters.
     */
    public NxBehavior(String name) {
        this.name = name;
        this.index = nextIndex++;
    }

    /**
     * Set function for gain.
     * @param gain value of the gain of the behavior. Values permited are between 0 and 1.
     */
    public void setGain(float gain)
    {

        if(gain >= 0 && gain <= 1)
            this.gain = gain;
    }

    /**
     * Get function for gain.
     * @return The gain of this behavior.
     */
    public float getGain(){
        return gain;
    }

    /**
     * Set function for priority.
     * @param priority value of the priority of the behavior. Values permited are between 0 and 100.
     */
    public void setPriority(int priority)
    {
        if(priority >= 0 && priority <= 100)
            this.priority = priority;
    }

    /**
     * Get function for priority.
     * @return The priority of this behavior.
     */
    public int getPriority()
    {
        return this.priority;
    }

    /**
     * Get function for index.
     * @return The index of this behavior.
     */
    public int getIndex()
    {
        return this.index;
    }

    /**
     * static Get function for nexIndex.
     * @return The next index that will be assigned to next behavior.
     */
    static public int getNextIndex()
    {
        return nextIndex;
    }

    /**
     * Get function for name.
     * @return The name of this behavior.
     */
    public String getName()
    {
        return this.name;
    }


    /**
     * This API implements potential fields,that is why it is vital that each
     * Behavior returns a Vector in 2D as its result.
     * @return The returned value of the behavior.
     */
    public abstract Object value();

    @Override
    public int compareTo(Object o)
    {
        int prior1 = ((NxBehavior)o).getPriority();

        if(prior1 < this.getPriority())
            return 1;
        else if(prior1 == this.getPriority())
            return -1;
        else
            return 0;

    }
}
